/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/11/25 19:58
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */


(function () {

    "use strict";

    var api = {
        getSummary: '/home/getSummary',
        getSummaryChart: '/home/getSummaryChart',
        getLatest: '/home/getLatest'
    };

    sm.controller("admin-home-index", ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        $scope.summary = {
            total: {
                'order': 0,
                'product': 0,
                'user': 0,
                'bbs': 0,
                'install': 0,
                'base': 0,
                'startup': 0,
                'pv': 0
            },
            today: {
                'order': 0,
                'install': 0,
                'user': 0,
                'bbs': 0
            }
        };
        $scope.latestUsers = [];
        $scope.latestBases = [];
        $scope.latestApp = [];
        $scope.latestOrders = [];

        $http.get(api.getSummary).success(function (res) {
            if (res.state) {
                $scope.summary.total = res.data.total;
                $scope.summary.today = res.data.today;
            }
        });

        $http.get(api.getLatest).success(function (res) {
            if (res.state) {
                $scope.latestUsers = res.data.users;
                $scope.latestBases = res.data.bases;
                $scope.latestProducts = res.data.products;
                $scope.latestOrders = res.data.orders;
            }
        });

        $http.get(api.getSummaryChart).success(function (res) {

            if (res.state) {
                doSummaryChart(res.data.label.summary, res.data.data.summary);
                doUserChart(res.data.data.user);
                doAppChart(res.data.label.app, res.data.data.app);
            }
        });

        function doSummaryChart(label, data) {
            var labels = [], user = [], bbs = [], app = [], order = [];
            angular.forEach(label, function (val) {
                labels.push(val);
            });

            angular.forEach(data, function (val, k) {
                angular.forEach(val, function (v) {
                    switch (k) {
                        case 'user':
                            user.push(v);
                            break;
                        case 'app':
                            app.push(v);
                            break;
                        case 'bbs':
                            bbs.push(v);
                            break;
                        case "order":
                            order.push(v);
                            break;
                    }
                });
            });

            var areaChartData = {
                labels: labels,
                datasets: [
                    {
                        label: "订单",
                        fillColor: "rgba(0, 192, 239, 0.9)",
                        strokeColor: "rgba(0, 192, 239, 0.8)",
                        pointColor: "rgba(0, 192, 239, 1)",
                        pointStrokeColor: "#c1c7d1",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(0, 192, 239, 1)",
                        data: order
                    },
                    {
                        label: "客户端",
                        fillColor: "rgba(60,141,188,0.9)",
                        strokeColor: "rgba(60,141,188,0.8)",
                        pointColor: "#3b8bba",
                        pointStrokeColor: "rgba(60,141,188,1)",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(60,141,188,1)",
                        data: app
                    },
                    {
                        label: "会员",
                        fillColor: "rgba(243, 156, 18, 0.9)",
                        strokeColor: "rgba(243, 156, 18,0.8)",
                        pointColor: "#3b8bba",
                        pointStrokeColor: "rgba(243, 156, 18, 1)",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(243, 156, 18, 1)",
                        data: user
                    },
                    {
                        label: "帖子",
                        fillColor: "rgba(221, 75, 57, 0.9)",
                        strokeColor: "rgba(221, 75, 57, 0.8)",
                        pointColor: "#3b8bba",
                        pointStrokeColor: "rgba(221, 75, 57, 1)",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(221, 75, 57, 1)",
                        data: bbs
                    }
                ]
            };


            var barChartCanvas = $("#barChart").get(0).getContext("2d");
            var barChart = new Chart(barChartCanvas);
            var barChartData = areaChartData;
            barChartData.datasets[1].fillColor = "#00a65a";
            barChartData.datasets[1].strokeColor = "#00a65a";
            barChartData.datasets[1].pointColor = "#00a65a";
            var barChartOptions = {
                //Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
                scaleBeginAtZero: true,
                //Boolean - Whether grid lines are shown across the chart
                scaleShowGridLines: true,
                //String - Colour of the grid lines
                scaleGridLineColor: "rgba(0,0,0,.05)",
                //Number - Width of the grid lines
                scaleGridLineWidth: 1,
                //Boolean - Whether to show horizontal lines (except X axis)
                scaleShowHorizontalLines: true,
                //Boolean - Whether to show vertical lines (except Y axis)
                scaleShowVerticalLines: true,
                //Boolean - If there is a stroke on each bar
                barShowStroke: true,
                //Number - Pixel width of the bar stroke
                barStrokeWidth: 2,
                //Number - Spacing between each of the X value sets
                barValueSpacing: 5,
                //Number - Spacing between data sets within X values
                barDatasetSpacing: 1,
                //String - A legend template
                legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].fillColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>",
                //Boolean - whether to make the chart responsive
                responsive: true,
                maintainAspectRatio: true
            };

            barChartOptions.datasetFill = false;
            barChart.Bar(barChartData, barChartOptions);
        }
        var areaChartOptions = {
            //Boolean - If we should show the scale at all
            showScale: true,
            //Boolean - Whether grid lines are shown across the chart
            scaleShowGridLines: false,
            //String - Colour of the grid lines
            scaleGridLineColor: "rgba(0,0,0,.05)",
            //Number - Width of the grid lines
            scaleGridLineWidth: 1,
            //Boolean - Whether to show horizontal lines (except X axis)
            scaleShowHorizontalLines: true,
            //Boolean - Whether to show vertical lines (except Y axis)
            scaleShowVerticalLines: true,
            //Boolean - Whether the line is curved between points
            bezierCurve: true,
            //Number - Tension of the bezier curve between points
            bezierCurveTension: 0.3,
            //Boolean - Whether to show a dot for each point
            pointDot: false,
            //Number - Radius of each point dot in pixels
            pointDotRadius: 4,
            //Number - Pixel width of point dot stroke
            pointDotStrokeWidth: 1,
            //Number - amount extra to add to the radius to cater for hit detection outside the drawn point
            pointHitDetectionRadius: 20,
            //Boolean - Whether to show a stroke for datasets
            datasetStroke: true,
            //Number - Pixel width of dataset stroke
            datasetStrokeWidth: 2,
            //Boolean - Whether to fill the dataset with a color
            datasetFill: true,
            //String - A legend template
            legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].lineColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>",
            //Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
            maintainAspectRatio: true,
            //Boolean - whether to make the chart responsive to window resizing
            responsive: true
        };

        function doAppChart(label, data)
        {
            var labels = [], ios = [], android = [];

            angular.forEach(label, function (val) {
                labels.push(val);
            });

            angular.forEach(data.ios, function (val) {
                ios.push(parseInt(val));
            });
            angular.forEach(data.android, function (val) {
                android.push(parseInt(val));
            });

            // LINE CHART
            var areaChartData = {
                labels: labels,
                datasets: [
                    {
                        label: "IOS安装量",
                        fillColor: "rgba(210, 214, 222, 1)",
                        strokeColor: "rgba(210, 214, 222, 1)",
                        pointColor: "rgba(210, 214, 222, 1)",
                        pointStrokeColor: "#c1c7d1",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(220,220,220,1)",
                        data: ios
                    },
                    {
                        label: "安卓安装量",
                        fillColor: "rgba(60,141,188,0.9)",
                        strokeColor: "rgba(60,141,188,0.8)",
                        pointColor: "#3b8bba",
                        pointStrokeColor: "rgba(60,141,188,1)",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(60,141,188,1)",
                        data: android
                    }
                ]
            };

            var lineChartCanvas = $("#lineChart").get(0).getContext("2d");
            var lineChart = new Chart(lineChartCanvas);
            var lineChartOptions = areaChartOptions;
            lineChartOptions.datasetFill = false;
            lineChart.Line(areaChartData, lineChartOptions);
        }

        //-------------
        //- LINE CHART -
        //--------------
        function doUserChart(data) {

            var pieChartCanvas = $("#pieChart").get(0).getContext("2d");
            var pieChart = new Chart(pieChartCanvas);
            var PieData = [
                {
                    value: data.wx,
                    color: "#00a65a",
                    highlight: "#00a65a",
                    label: "微信"
                },
                {
                    value: data.ios,
                    color: "#f39c12",
                    highlight: "#f39c12",
                    label: "IOS"
                },
                {
                    value: data.android,
                    color: "#00c0ef",
                    highlight: "#00c0ef",
                    label: "安卓"
                }
            ];
            var pieOptions = {
                segmentShowStroke: true,
                segmentStrokeColor: "#fff",
                segmentStrokeWidth: 2,
                percentageInnerCutout: 50, // This is 0 for Pie charts
                animationSteps: 100,
                animationEasing: "easeOutBounce",
                animateRotate: true,
                animateScale: true,
                responsive: true,
                maintainAspectRatio: false,
                legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>",
                tooltipTemplate: "<%=value %> <%=label%> 用户"
            };
            pieChart.Doughnut(PieData, pieOptions);
        }


    }]);



})();
